﻿using Delphi.Isis.Yahoo;
using Delphi.Mimir.Config;
using Delphi.Mimir.Data.Entities.Final;
using Delphi.Mimir.Data.Entities.Raw;
using System.Collections.Generic;

namespace Delphi.Mimir.Wrapper {
    public static class FinancialStatementWrapper {

        public static void AddStatement(List<YahooIncomeStatement> yahooIncomeStatements) {
            using (var context = new DelphiContext()) {
                foreach(YahooIncomeStatement yis in yahooIncomeStatements) {
                    Symbol sym = SymbolWrapper.GetSymbol(yis.YahooCode);
                    RawIncomeStatement ris = new RawIncomeStatement();

                    ris.sis_sym_cd = sym.Code;
                    ris.sis_exc_cd = sym.Exchange.Code;
                    ris.sis_period = yis.Period;
                    ris.sis_dt = yis.Date;
                    ris.sis_tot_revenue = yis.TotalRevenue * 1000;
                    ris.sis_cogs = yis.CostOfRevenue * 1000;
                    ris.sis_gross_profit = yis.GrossProfit * 1000;
                    ris.sis_rnd = yis.RandD * 1000;
                    ris.sis_sga = yis.SGA * 1000;
                    ris.sis_non_rec_exp = yis.NonRecurring * 1000;
                    ris.sis_other_exp = yis.OtherExpenses * 1000;
                    ris.sis_tot_opex = yis.TotalOpex * 1000;
                    ris.sis_op_income = yis.OperatingIncome * 1000;
                    ris.sis_tot_other_income = yis.TotalOtherIncome * 1000;
                    ris.sis_ebit = yis.Ebit * 1000;
                    ris.sis_interest_expense = yis.InterestExpense * 1000;
                    ris.sis_ebt = yis.Ebt * 1000;
                    ris.sis_income_tax_expense = yis.IncomeTaxExpenses * 1000;
                    ris.sis_minority_interest = yis.MinorityInterest * 1000;
                    ris.sis_ni_continuing_ops = yis.NiFromContinuingOps * 1000;
                    ris.sis_discnt_ops = yis.DiscontinuedOps * 1000;
                    ris.sis_extraordinary_items = yis.ExtraordinaryItems * 1000;
                    ris.sis_accounting_changes = yis.EffectOfAccountingChanges * 1000;
                    ris.sis_other_items = yis.OtherItems * 1000;
                    ris.sis_ni = yis.NetIncome * 1000;
                    ris.sis_prfrd_stock_adjust = yis.PreferredStockAndOtherAdjustments * 1000;
                    ris.sis_ni_common = yis.NiAttributableToCommon * 1000;

                    context.RawIncomeStatements.Add(ris);
                    context.SaveChanges();
                }
            }
        }

        public static void AddStatement(List<YahooBalanceSheet> yahooBalanceSheets) {
            using (var context = new DelphiContext()) {
                foreach (YahooBalanceSheet y in yahooBalanceSheets) {
                    Symbol sym = SymbolWrapper.GetSymbol(y.YahooCode);
                    RawBalanceSheet db = new RawBalanceSheet();

                    db.sbs_sym_cd = sym.Code;
                    db.sbs_exc_cd = sym.Exchange.Code;
                    db.sbs_period = y.Period;
                    db.sbs_dt = y.Date;
                    
                    db.sbs_ca_cash_and_eqv = y.CashAndEquivalents * 1000;
                    db.sbs_ca_st_inv = y.ShortTermInvestments * 1000;
                    db.sbs_ca_net_recbls = y.NetReceivables * 1000;
                    db.sbs_ca_invtry = y.Inventory * 1000;
                    db.sbs_ca_other_assts = y.OtherCurrentAssets * 1000;
                    db.sbs_ca_tot = y.TotalCurrentAssets * 1000;
                    db.sbs_nca_lt_inv = y.LongTermInvestments * 1000;
                    db.sbs_nca_ppe = y.Ppe * 1000;
                    db.sbs_nca_goodwill = y.Goodewill * 1000;
                    db.sbs_nca_intgbl_assts = y.IntangibleAssets * 1000;
                    db.sbs_nca_accum_amort = y.AccumulatedAmort * 1000;
                    db.sbs_nca_other_assts = y.OtherAssets * 1000;
                    db.sbs_nca_dfrd_lt_assts_chrg = y.DeferredLtAssetCharges * 1000;
                    db.sbs_tot_assts = y.TotalAssets * 1000;
                    db.sbs_cl_acct_paybl = y.AccountsPayable * 1000;
                    db.sbs_cl_crnt_lt_debt = y.CurrentDebt * 1000;
                    db.sbs_cl_other = y.OtherCurrentLiabilities * 1000;
                    db.sbs_cl_total = y.TotalCurrentLiabilities * 1000;
                    db.sbs_ncl_lt_debt = y.LongTermDebt * 1000;
                    db.sbs_ncl_other_liab = y.OtherLtLiabilities * 1000;
                    db.sbs_ncl_dfrd_lt_liab_chrg = y.DefferLtLiabilitiesCharges * 1000;
                    db.sbs_ncl_min_intr = y.MinorityInterest * 1000;
                    db.sbs_ncl_neg_goodwill = y.NegativeGoodwill * 1000;
                    db.sbs_tot_liab = y.TotalLiabilities * 1000;
                    db.sbs_eq_misc_st_opt_wrnt = y.StockWarrants * 1000;
                    db.sbs_eq_redmbl_prfrd_st = y.RedeemablePreferredStock * 1000;
                    db.sbs_eq_prfrd_st = y.PreferredStock * 1000;
                    db.sbs_eq_comn_st = y.CommonStock * 1000;
                    db.sbs_eq_rtnd_earn = y.RetainedEarnings * 1000;
                    db.sbs_eq_trsry_st = y.TreasuryStock * 1000;
                    db.sbs_eq_cap_spls = y.CapitalSurplus * 1000;
                    db.sbs_eq_other_eqty = y.OtherStockholderEquity * 1000;
                    db.sbs_eq_comn_st = y.TotalStockholderEquity * 1000;
                    db.sbs_net_tgbl_assts = y.NetTangibleAssets * 1000;
                    
                    context.RawBalanceSheets.Add(db);
                    context.SaveChanges();
                }
            }
        }

        public static void AddStatement(List<YahooCashFlow> yahooCashFlows) {
            using (var context = new DelphiContext()) {
                foreach (YahooCashFlow y in yahooCashFlows) {
                    Symbol sym = SymbolWrapper.GetSymbol(y.YahooCode);
                    RawCashFlow db = new RawCashFlow();

                    db.scf_sym_cd = sym.Code;
                    db.scf_exc_cd = sym.Exchange.Code;
                    db.scf_period = y.Period;
                    db.scf_dt = y.Date;

                    db.scf_depr = y.Depretiation * 1000;
                    db.scf_adj_ni = y.AdjustmentsNi * 1000;
                    db.scf_chng_acct_recbls = y.ChangesReceivables * 1000;
                    db.scf_chng_liab = y.ChangesLiabilities * 1000;
                    db.scf_chng_invtry = y.ChangesInventories * 1000;
                    db.scf_chng_other_ops = y.ChangesOtherOps * 1000;
                    db.scf_tot_cfo = y.TotalCfo * 1000;
                    db.scf_capex = y.CapEx * 1000;
                    db.scf_inv = y.Investments * 1000;
                    db.scf_other_inv = y.OtherInvesting * 1000;
                    db.scf_tot_cfi = y.TotalCfi * 1000;
                    db.scf_div_paid = y.DividendsPaid * 1000;
                    db.scf_sal_purch_st = y.SalePurchaseStock * 1000;
                    db.scf_net_borrow = y.NetBorrowings * 1000;
                    db.scf_other_fin = y.OtherFinancing * 1000;
                    db.scf_tot_cff = y.TotalCff * 1000;
                    db.scf_fx_rate_chng = y.EffectFxRateChangess * 1000;
                    db.scf_chng_cash_and_eqv = y.ChangeCashAndEquivalents * 1000;


                    context.RawCashFlows.Add(db);
                    context.SaveChanges();
                }
            }
        }
    }
}
